package simpleim

import (
	"errors"
	"github.com/gorilla/websocket"
	"sync"
)

// ConnStore 简易版本的注册中心
type ConnStore struct {
	conns sync.Map
}

// Store 存储映射
func (store *ConnStore) Store(uid int64, conn *websocket.Conn) {
	store.conns.Store(uid, conn)
}

// Load 查找映射
func (store *ConnStore) Load(uid int64) (*websocket.Conn, error) {
	load, ok := store.conns.Load(uid)
	if !ok {
		return nil, errors.New("不存在")
	}
	return load.(*websocket.Conn), nil
}

// Delete 移除映射
func (store *ConnStore) Delete(uid int64) {
	store.conns.Delete(uid)
}
